package com.qf.day07.poly01;

import java.util.Random;

/**
 * 武器工具类
 */
public class WeaponryUtils {
    public static void main(String[] args) {
        Weaponry[] randomWeaponries = WeaponryUtils.creatRandomWeapon(5);
        for (int i = 0; i < randomWeaponries.length; i++) {
            System.out.println(randomWeaponries[i]);
        }
        System.out.println("============");
        //对武器进行分类
        Sword[] swords = new Sword[randomWeaponries.length];
        Dagger[] daggers = new Dagger[randomWeaponries.length];
        WeaponryUtils.typeWeapon(randomWeaponries,swords,daggers);
        for (Sword sword:swords){
            System.out.println(sword);
        }
        for (Dagger dagger:daggers){
            System.out.println(dagger);
        }
    }
    /**
     * 将随机出来的武器进行分类
     * @param weaponries    武器数组
     * @param swords    长剑数组
     * @param daggers   匕首数组
     */
    public static void typeWeapon(Weaponry[] weaponries,Sword[] swords,Dagger[] daggers){
        int swordIndex=0;
        int daggerIndex=0;
        for (int i = 0; i < weaponries.length; i++) {
            Weaponry weaponry = weaponries[i];
            if(weaponry instanceof Sword){
                Sword sword = (Sword) weaponries[i];
                swords[swordIndex++]=sword;
            }
            if(weaponry instanceof Dagger){
                Dagger dagger= (Dagger) weaponries[i];
                daggers[daggerIndex++]=dagger;
            }
        }

    }
    /**
     * 创建随机数量的武器
     * @param num   武器数量
     * @return weaponries 武器数组
     */
    public static Weaponry[] creatRandomWeapon(int num){
        //创建用来存放武器的数组
        Weaponry[] weaponries= new Weaponry[num];
        String[] daggerNames={"清刚","扬文","龙鳞","徐氏匕首","梅花匕","羊角匕首"};
        String[] swordNames={"轩辕剑","赤霄剑","倚天剑","鱼肠剑","湛卢剑","干将","莫邪"};
        for (int i = 0; i < num; i++) {
            Random rand = new Random();
            int randomNum = rand.nextInt(2); // 生成0或1的随机数
            switch (randomNum){
                case 0:
                    //创建匕首
                    Dagger dagger = new Dagger();
                    dagger.setName(daggerNames[rand.nextInt(daggerNames.length)]);
                    dagger.setAttack(1+rand.nextInt(10));
//                    把创建好的匕首放入数组中
                    weaponries[i]=dagger;
                    break;
                case 1:
                    //创建长剑
                    Sword sword = new Sword();
                    sword.setName(swordNames[rand.nextInt(swordNames.length)]);
                    sword.setAttack(1+rand.nextInt(10));
                    //将创建好的长剑放进数组
                    weaponries[i]=sword;
                    break;
            }
        }
        return weaponries;
    }
}
